//
//  DDCreateOrderResultViewController.m
//  Dingdong
//
//  Created by 程强 on 16/11/15.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DDCreateOrderResultViewController.h"
#import "DDGoodsCollectionCell.h"
#import "MJRefresh.h"
#import "DDHomeManager.h"
#import "DDHotSaleSet.h"
#import "DDHotSaleCollectionCell.h"
#import "DDGoodsDetailsViewController.h"

@interface DDCreateOrderResultViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollWidth;
@property (weak, nonatomic) IBOutlet UICollectionView *saleCollection;
@property (weak, nonatomic) IBOutlet UIScrollView *bgScrollView;
@property (strong, nonatomic) DDHomeManager *manager;
@property (assign, nonatomic) NSInteger currentPage;
@property (strong, nonatomic) DDHotSaleSet *hotSaleSet;
@end

@implementation DDCreateOrderResultViewController
- (void)gotoBack
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.manager = [DDHomeManager new];
    [self layoutResultView];
}
- (void)layoutResultView
{
    CGFloat width = (ScreenWidth-20)/2.0;
    CGFloat height = width+50+5;
    self.scrollWidth.constant = ScreenWidth;
    self.scrollHeight.constant = 300+height*2;
    [_saleCollection registerClass:[DDGoodsCollectionCell class] forCellWithReuseIdentifier:@"HotSaleCell"];
    self.currentPage = 1;
    [self setRefresh];
}
- (void)setRefresh
{
    weak_Self(self);
    MJRefreshBackNormalFooter *footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakSelf footerRefresh];
    }];
    [self.bgScrollView setMj_footer:footer];
    [self footerRefresh];
}
- (void)footerRefresh
{
    weak_Self(self);
    [self fetchHotSaleListComplete:^{
        [weakSelf.bgScrollView.mj_footer endRefreshing];
    }];
}
- (void)fetchHotSaleListComplete:(void(^)())block
{
    weak_Self(self);
    [[self.manager getHomeVerticalGoodsListWithPageNo:self.currentPage size:kPageSize]subscribeNext:^(id x) {
        if (!weakSelf.hotSaleSet) {
            weakSelf.hotSaleSet = x;
        }else{
            DDHotSaleSet *set = x;
            [weakSelf.hotSaleSet.goodsGroup addObjectsFromArray:set.goodsGroup];
        }
        weakSelf.currentPage+=1;
        [weakSelf reloadHotSaleGoods];
    } error:^(NSError *error) {
        
    } completed:^{
        if (block) {
            block();
        }
    }];
}
- (void)reloadHotSaleGoods
{
    [self.saleCollection reloadData];
    CGFloat width = (ScreenWidth-10)/2.0;
    CGFloat height = width+50;
    
    NSInteger row = 0;
    NSInteger remainder = self.hotSaleSet.goodsGroup.count%2;
    if (remainder == 0) {
        row =self.hotSaleSet.goodsGroup.count/2;
    }else{
        row =self.hotSaleSet.goodsGroup.count/2+1;
    }
    
    self.scrollHeight.constant = 250+50+row*(height+5);
    
}
#pragma mark --- UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.hotSaleSet.goodsGroup.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DDHotSaleCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"OrderHotSaleCell" forIndexPath:indexPath];
    DDGoodsObj *obj = self.hotSaleSet.goodsGroup[indexPath.row];
    [cell.hotSaleGoodsIcon sd_setImageWithURL:[NSURL URLWithString:obj.thumbnail] placeholderImage:nil];
    cell.hotSaleGoodsName.text = obj.name;
    cell.hotSaleGoodsPrice.text = FormatString(@"$%.2f", [obj.price doubleValue]);
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    DDGoodsDetailsViewController *vc = [[DDGoodsDetailsViewController alloc]init];
    DDGoodsObj *obj = self.hotSaleSet.goodsGroup[indexPath.row];
    vc.goods_id = obj.goods_id;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark ---- UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width = (ScreenWidth-10)/2.0;
    CGFloat height = width+50;
    return (CGSize){width,height};
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 2, 5, 2);
}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 5.f;
}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 5.f;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionReusableView *reusableView = nil;
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView * header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
        reusableView = header;
    }
    
    return reusableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
